﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace Scope8
{
    /// <summary>
    /// Diese Klasse repräsentiert einen AperioSlide. Ein Slide ist nach unseren Vorstellungen ein Objekt, das einem Bild sehr ähnlich kommt. Es wird aber zusätlich von einem 
    /// Web-Slide-Server abgefragt, der eine bestimmte Auflösung anhand der Abfrageparamter ermittelt. 
    /// </summary>
    class AperioSlide : Slide
    {
        /// <summary>
        /// das Zielverhältnis von Höhe druch Breite. HW= höhe durch breite WH=breite durch höhe
        /// </summary>
        private double targetRatio_HW = 0;
        /// <summary>
        /// das Zielverhältnis von Breite druch Höhe. HW= höhe durch breite WH=breite durch höhe
        /// </summary>
        private double targetRatio_WH = 0;
        /// <summary>
        /// das Slideverhältnis von Höhe druch Breite. HW= höhe durch breite WH=breite durch höhe
        /// </summary>
        private double slideRatio_HW = 0;
        /// <summary>
        /// das Slideverhältnis von Breite druch Höhe. HW= höhe durch breite WH=breite durch höhe
        /// </summary>
        private double slideRatio_WH = 0;

        /// <summary>
        /// skalierte Breite des Bildausschnitts34)
        /// </summary>
        private int scrw = 0;
        /// <summary>
        /// skalierte Höhe des Bildausschnitts34)
        /// </summary>
        private int scrh = 0;
        /// <summary>
        /// linke Kante des Bildausschnitts: 0 … gesamte Breite - slidew/2
        /// </summary>
        private int posX = 0;
        /// <summary>
        /// 
        /// obere Kante des Bildausschnitts: 0 … gesamte Höhe - slideh/2
        /// </summary>
        private int posY = 0;

        /// <summary>
        /// Zoomvariabledes Aperioservers
        /// </summary>
        private double zoom = 100;



        //image?x+y+w+h+z+q+m+f+r
        //image?x+y+scrw+scrh+z+q+m+f+r
        /// <summary>
        /// Initialisiert eine Instanz der Klasse.
        /// </summary>
        public AperioSlide() : base()
        {
            setServerURL(AperioConstants.aperioURL_pure);
            setThumbNails("?0+0+250+250+-1+80");
        }

        //// <summary>
        /// Erzeugt eine Instanz der Klasse mit den angegebenen Werten
        /// </summary>
        /// <param name="newPicName">der Bildname</param>
        /// <param name="newWidth">die max. Breite des Bildes</param>
        /// <param name="newHeight">die max. Höhe des Bildes</param>
        public AperioSlide(String newPicName, int newWidth, int newHeight)
            : base(newPicName, newWidth, newHeight)
        {
            setServerURL(AperioConstants.aperioURL_pure);
        }

        /// <summary>
        /// Intialisiert das Slide. 
        /// </summary>
        public override String InitializeSlide(double viewfildwidth, double viewfildheight)
        {
            //Aktuelle Auflösung abfragen abzüglich des Topmenüs
            //double viewfildwidth = (Window.Current.Bounds.Width);
            //double viewfildheight = (Window.Current.Bounds.Height - 140);

            //Seitenverhältnisse berechnen
            targetRatio_WH = viewfildwidth / viewfildheight;
            targetRatio_HW = viewfildheight / viewfildwidth;
            slideRatio_WH = (double)getWidth() / (double)getHeight();
            slideRatio_HW = (double)getHeight() / (double)getWidth();

            //Je nach Seitenverhältnis Initialisieren
            if (targetRatio_HW < slideRatio_HW)
            {//Links und rechts Rand(hochformat)
                double maxZoomSlideh = getHeight() / 100;
                scrh = (int) viewfildheight;
                scrw = (int)(scrh * slideRatio_WH);
                zoom = (double)getHeight() / (double)scrh;
                
            }
            else if (targetRatio_HW > slideRatio_HW)
            {
                double maxZoomSlidew = getWidth() / 100;
                scrw = (int) viewfildwidth;
                scrh = (int)(scrw * slideRatio_HW);
                zoom = (double)getWidth() / (double)scrw;
                
            }
            else
            {
                double maxZoomSlidew = getWidth() / 100;
                scrh = (int)viewfildheight;
                scrw = (int)viewfildwidth;
                zoom = (double)getHeight() / (double) scrh;
            }
            posX = 0;
            posY = 0;

            return getManipulatedUrl();
        }


        /// <summary>
        /// Liefert die vollständige URL mit zusätzlichen Abfrageparametern z.B. scrh usw. 
        /// //image?x+y+w+h+z+q+m+f+r
        /// x  X-coordinateof region, in pixels (upper left is [0,0]). Default is 0. 
        /// y  Y-coordinateof region, in pixels (upper left is [0,0]). Default is 0. 
        /// w  widthof region, in pixels. Maximum width is 2000 pixels. Default is image width or 2000, whichever is less. 
        /// h  heightof region, in pixels. Maximum heightis 2000 pixels. Default is image height or 2000, whichever is less. 
        /// z  zoomlevel of region (float), .5 = 200%, 2 = 50%, 4 = 25%. Other special values: 
        /// -1 = thumbnail image 
        /// -2 = label image (if any)
        /// -3 = macro camera image (if any) 
        /// The default value for zoom is 1 (= 100%) 
        /// q JPEG qualitylevel, (1-100). A level of 75 approximately corresponds to the JPEG2000 image quality. Default is 75. 
        /// m JPEG mode. If specified as Simages will be static (non-progressive). Default is progressive (P). {Ordinarily progressive mode is“better” however some software cannot handle progressive JPEGs, notably Macromedia Flash.} 
        /// f  focusvalue for region (float), in range -1 ->+1 (with 0 at “center”). Default is 0. 
        /// r  randomstring value. Not used by ImageServer, but can be used to ensure that the URL is unique. This is useful for defeating browser caching. 
        /// 
        ///  image?x+y+scrw+scrh+z+q+m+f+r
        /// </summary>
        /// <returns></returns>
        public override String getManipulatedUrl()
        {
            String zoomConvert = zoom.ToString();
            string[] splitZoom = zoomConvert.Split(new Char[] { ',' });
            String url = getfullUrl() + "?" + posX + "+" + posY + "+" + scrw + "+" + scrh + "+"+ splitZoom[0]+"."+splitZoom[1];
            return url;
        }
    }
}
